package com.xuecheng.content.controller;

import com.xuecheng.api.contant.TeachplanApi;
import com.xuecheng.api.contant.model.dto.TeachplanDTO;
import com.xuecheng.api.contant.model.vo.TeachplanVO;
import com.xuecheng.api.media.model.TeachplanMediaDTO;
import com.xuecheng.api.media.model.vo.BindTeachplanMediaVO;
import com.xuecheng.common.util.SecurityUtil;
import com.xuecheng.content.convert.TeachplanConvert;
import com.xuecheng.content.service.TeachplanService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 课程计划 前端控制器
 * </p>
 *
 * @author itcast
 */
@Slf4j
@RestController
public class TeachplanController implements TeachplanApi {

    @Autowired
    private TeachplanService  teachplanService;

    @PostMapping("teachplan/media/association")
    public TeachplanMediaDTO associateMedia(@RequestBody BindTeachplanMediaVO vo) {
        // 1、获得访问令牌并从中解析出机构的信息Id数据
        Long companyId = SecurityUtil.getCompanyId();

        // 2、创建课程计划媒资信息dto对象
        TeachplanMediaDTO teachplanMediaDTO = new TeachplanMediaDTO();

        // 3、赋值
        teachplanMediaDTO.setTeachplanId(vo.getTeachplanId());
        teachplanMediaDTO.setMediaId(vo.getMediaId());

        // 4、调用方法
        TeachplanMediaDTO resultDTO = teachplanService.associateMedia(teachplanMediaDTO, companyId);

        // 5、返回数据
        return resultDTO;
    }
    /**
     * 根据Id查询出相关的课程计划并封装到TeachplanNode
     * @param courseId 课程基础信息id
     * @return TeachplanDTO 封装TeachplanNode
     */
    @GetMapping("teachplan/{courseId}/tree-nodes")
    public TeachplanDTO queryTreeNodesByCourseId(@PathVariable Long courseId){
        // 1、获得访问令牌并从中解析出机构的信息Id数据
        Long companyId = SecurityUtil.getCompanyId();

        // 2、调用方法
        TeachplanDTO teachplanDTO = teachplanService.queryTreeNodesByCourseId(courseId, companyId);

        // 3、返回dto
        return teachplanDTO;
    }

    /**
     * 根据课程计划Id删除课程计划信息
     * @param teachPlanId {@link Long} 课程计划Id
     */
    @DeleteMapping("teachplan/{teachplanid}")
    public void removeTeachPlan(@PathVariable("teachplanid") Long teachPlanId) {
        // 1.获得公司Id
        Long companyId = SecurityUtil.getCompanyId();

        // 2.调用service方法
        teachplanService.removeTeachPlan(teachPlanId,companyId);
    }

    /**
     * 新增或修改课程计划
     * @param teachplanVO 课程计划封装类型数据
     * @return teachplanDTO 课程计划封装类型返回数据
     */
    @PostMapping("teachplan")
    public TeachplanDTO createOrModifyTeachPlan(@RequestBody TeachplanVO teachplanVO) {
        //1.获得访问令牌并从中解析出机构的信息Id数据
        Long companyId = SecurityUtil.getCompanyId();

        //2、将vo转成dto
        TeachplanDTO teachplanDTO = TeachplanConvert.INSTANCE.vo2dto(teachplanVO);

        //3、调用service层方法
        TeachplanDTO dto = teachplanService.createModifyTeachplan(teachplanDTO,companyId);

        //4、返回dto数据
        return dto;
    }
}
